package com.pansky.crm.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pansky.crm.annotation.OperateLog;
import com.pansky.crm.common.mybatisplus.ApiResult;
import com.pansky.crm.common.mybatisplus.ResultPageDto;
import com.pansky.crm.entity.Employee;
import com.pansky.crm.entity.bo.EmployeeBatchUpdateStatusDTO;
import com.pansky.crm.entity.bo.EmployeePageBo;
import com.pansky.crm.entity.bo.EmployeeUpdatePwdDTO;
import com.pansky.crm.entity.vo.EmployeeVo;
import com.pansky.crm.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/2/26 9:06
 * @description：员工controller
 */
@Slf4j
@RestController
@RequestMapping("/employee")
@OperateLog
@Api(tags = {"管理端-员工api"})
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/query")
    @ApiOperation(value = "员工管理查询", notes = "员工管理查询")
    public ApiResult<ResultPageDto> queryEmployee(@RequestBody EmployeePageBo employeeQueryBo) {
        log.info("queryEmployee employee {}", employeeQueryBo);
        return ApiResult.ok(employeeService.queryByPage(employeeQueryBo));
    }

    @ApiOperation(value = "禁用单个员工", notes = "禁用单个员工")
    @GetMapping("/updateStatus")
    public ApiResult<Integer> updateStatus(@RequestParam Integer employeeId, @RequestParam Integer status) {
        return ApiResult.ok(employeeService.updateStatus(employeeId, status));
    }

    @ApiOperation(value = "批量启用/禁用员工", notes = "批量启用/禁用员工")
    @PostMapping("/batchUpdateStatus")
    public ApiResult<Boolean> batchUpdateStatus(@Valid @RequestBody EmployeeBatchUpdateStatusDTO batchUpdateStatusDTO) {
        return ApiResult.ok(employeeService.batchUpdateStatus(batchUpdateStatusDTO));
    }

    @ApiOperation(value = "员工重置密码", notes = "员工重置密码")
    @GetMapping("/resetPasswd")
    public ApiResult<String> resetPasswd(@RequestParam Integer employeeId) {
        if(employeeService.resetPasswd(employeeId)) {
            return ApiResult.ok();
        }
        return ApiResult.failed("重置密码失败！");
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping("/updatePwd")
    public ApiResult<String> updatePwd(@Valid @RequestBody EmployeeUpdatePwdDTO updatePwdDTO, @RequestHeader String token) {
        if(employeeService.updatePwd(updatePwdDTO, token)) {
            return ApiResult.ok();
        }
        return ApiResult.failed("修改密码失败！");
    }

    @ApiOperation(value = "角色成员列表中批量员工管理", notes = "角色成员列表中批量员工管理")
    @PostMapping("/manageEmployeeList")
    public ApiResult<Boolean> addEmployeeList(@Valid @RequestBody List<Integer> employeeList, @RequestParam Integer roleId) {
        return ApiResult.ok(employeeService.updateBatchRoleId(employeeList, roleId));
    }

    @ApiOperation(value = "从角色成员列表中批量移除员工", notes = "从角色成员列表中批量移除员工")
    @PostMapping("/removeEmployeeList")
    public ApiResult<Boolean> removeEmployeeList(@Valid @RequestBody List<Integer> employeeIdList) {
        return ApiResult.ok(employeeService.updateBatchRoleId(employeeIdList, null));
    }

    @ApiOperation(value = "从角色成员列表中移除员工", notes = "从角色成员列表中移除员工")
    @PostMapping("/removeEmployee")
    public ApiResult<Boolean> removeEmployee(@Valid @RequestParam Integer employeeId) {
        List<Integer> employeeIdList = new ArrayList<>();
        employeeIdList.add(employeeId);
        return ApiResult.ok(employeeService.updateBatchRoleId(employeeIdList, null));
    }

    @ApiOperation(value = "添加员工", notes = "添加员工")
    @PostMapping("/add")
    public ApiResult<Boolean> addEmployee(@Valid @RequestBody Employee emp, @RequestHeader String token) {
        return employeeService.addEmployee(emp, token);
    }

    @ApiOperation(value = "更新员工信息", notes = "@author yandanyang")
    @PostMapping("/update")
    public ApiResult<Boolean> updateEmployee(@Valid @RequestBody Employee employeeUpdateDto) {
        return ApiResult.ok(employeeService.updateById(employeeUpdateDto));
    }

    @ApiOperation(value = "删除员工信息", notes = "删除员工信息")
    @PostMapping("/delete")
    public ApiResult<Boolean> deleteEmployeeById(@RequestParam Integer employeeId) {
        return ApiResult.ok(employeeService.removeById(employeeId));
    }

    @ApiOperation(value = "单个员工角色授权", notes = "单个员工角色授权")
    @PostMapping("/updateRoles")
    public ApiResult<Boolean> updateRoles(@Valid @RequestParam Integer employeeId, @RequestParam Integer roleId) {
        List<Integer> employeeIdList = new ArrayList<>();
        employeeIdList.add(employeeId);
        return ApiResult.ok(employeeService.updateBatchRoleId(employeeIdList, roleId));
    }
}
